# Greeting people (with functions)
# Much better!
def greet(your_name):
print(f"Nice to meet you, {your_name}!")
greet("Alice")
greet("Bob")
greet("Charlie")Nice to meet you, Alice!
Nice to meet you, Bob!
Nice to meet you, Charlie!
Because copy-pasting code is so last semester
February 07, 2025
def keyword and reusable code blocksreturn, indent*args, and **kwargsreturn errors# Greeting people (without functions)
# Don't do this at home!
print("Nice to meet you, Alice!")
print("Nice to meet you, Bob!")
print("Nice to meet you, Charlie!")
print("Nice to meet you, Danilo!")
print("Nice to meet you, Emily!")
print("Nice to meet you, Frank!")
print("Nice to meet you, George!")
# ... and so on for 1000 names...print(), np.mean(), plt.hist() are all functions you’ve used before 🤓def keyword. Tells Python: “We are defining a function now”function_name). The name you’ll use to call the function (choose it wisely!)(). Hold values the function will use. Can be empty () if no input is neededparameter). A placeholder for the values the function will receive. Can be multiple, separated by commasparameter=argument). The actual values that will be passed to the function.:. Indicates the function header is complete. Don’t forget it!return statement (optional). The value the function sends back when it’s done. If absent, function returns Nonesay_greeting(name)return statement?message_alice = say_greeting("Alice")?say_greeting?greeting_type="Hello" part? What does it mean?message_bob = get_greeting("Bob") and then print(message)?get_greeting:def get_greeting(name, greeting_type="Hello"):
message = f"{greeting_type}, {name}! Nice to see you."
return message
message = get_greeting("Alice", "Hi")
print(message)Hi, Alice! Nice to see you.
calculate_area function below:length=5 and width=4, and print the result